import 'package:froom/froom.dart';

/// 账单实体 - 测试用
@Entity(tableName: 'bills')
class Bill {
  @PrimaryKey(autoGenerate: true)
  final int? id;

  @ColumnInfo(name: 'bill_no')
  final String billNo;

  @ColumnInfo(name: 'user_id')
  final int userId;

  @ColumnInfo(name: 'product_id')
  final int productId;

  final int quantity;

  @ColumnInfo(name: 'total_amount')
  final double totalAmount;

  final String status; // pending, paid, cancelled

  @ColumnInfo(name: 'created_at')
  final DateTime createdAt;

  @ColumnInfo(name: 'paid_at')
  final DateTime? paidAt;

  const Bill({
    this.id,
    required this.billNo,
    required this.userId,
    required this.productId,
    required this.quantity,
    required this.totalAmount,
    this.status = 'pending',
    required this.createdAt,
    this.paidAt,
  });

  Bill copyWith({
    int? id,
    String? billNo,
    int? userId,
    int? productId,
    int? quantity,
    double? totalAmount,
    String? status,
    DateTime? createdAt,
    DateTime? paidAt,
  }) {
    return Bill(
      id: id ?? this.id,
      billNo: billNo ?? this.billNo,
      userId: userId ?? this.userId,
      productId: productId ?? this.productId,
      quantity: quantity ?? this.quantity,
      totalAmount: totalAmount ?? this.totalAmount,
      status: status ?? this.status,
      createdAt: createdAt ?? this.createdAt,
      paidAt: paidAt ?? this.paidAt,
    );
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Bill &&
          runtimeType == other.runtimeType &&
          id == other.id &&
          billNo == other.billNo &&
          userId == other.userId &&
          productId == other.productId &&
          quantity == other.quantity &&
          totalAmount == other.totalAmount &&
          status == other.status;

  @override
  int get hashCode =>
      id.hashCode ^
      billNo.hashCode ^
      userId.hashCode ^
      productId.hashCode ^
      quantity.hashCode ^
      totalAmount.hashCode ^
      status.hashCode;

  @override
  String toString() {
    return 'Bill{id: $id, billNo: $billNo, userId: $userId, productId: $productId, quantity: $quantity, totalAmount: $totalAmount, status: $status}';
  }

  static String getTableName() => 'bills';
  static List<String> getPrimaryKey() => ['id'];
  static Map<String, String> getFieldCol() => {
        'id': 'id',
        'billNo': 'bill_no',
        'userId': 'user_id',
        'productId': 'product_id',
        'quantity': 'quantity',
        'totalAmount': 'total_amount',
        'status': 'status',
        'createdAt': 'created_at',
        'paidAt': 'paid_at',
      };
  static Bill toEntity(Map<String, Object?> map) => Bill(
        id: map['id'] as int?,
        billNo: map['bill_no'] as String,
        userId: map['user_id'] as int,
        productId: map['product_id'] as int,
        quantity: map['quantity'] as int,
        totalAmount: map['total_amount'] as double,
        status: map['status'] as String,
        createdAt: DateTime.parse(map['created_at'] as String),
        paidAt: map['paid_at'] != null
            ? DateTime.parse(map['paid_at'] as String)
            : null,
      );
  static Map<String, Object?> toMap(Bill bill) => {
        'id': bill.id,
        'bill_no': bill.billNo,
        'user_id': bill.userId,
        'product_id': bill.productId,
        'quantity': bill.quantity,
        'total_amount': bill.totalAmount,
        'status': bill.status,
        'created_at': bill.createdAt.toIso8601String(),
        'paid_at': bill.paidAt?.toIso8601String(),
      };
}
